package com.example.hospital.controller;

import com.example.hospital.common.ApiResponse;
import com.example.hospital.common.PageResponse;
import com.example.hospital.domain.dto.DoctorLoginRequest;
import com.example.hospital.domain.entity.Doctor;
import com.example.hospital.domain.entity.DoctorSchedule;
import com.example.hospital.domain.entity.ExaminationOrder;
import com.example.hospital.domain.entity.IndicatorResult;
import com.example.hospital.domain.entity.Sample;
import com.example.hospital.service.DoctorService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/api/doctors")
public class DoctorController {
    private final DoctorService doctorService;

    public DoctorController(DoctorService doctorService) { this.doctorService = doctorService; }

    /**
     * 医生登录
     * @param req
     * @return
     */
    @PostMapping("/login")
    public ApiResponse<Doctor> login(@Valid @RequestBody DoctorLoginRequest req) {
        return ApiResponse.success(doctorService.loginByPhone(req.getPhone(), req.getPassword()));
    }

    /**
     * 更新医生个人信息
     * @param doctorId
     * @param doctor
     * @return
     */
    @PutMapping("/{doctorId}")
    public ApiResponse<Boolean> updateProfile(@PathVariable("doctorId") Long doctorId, @RequestBody Doctor doctor) {
        doctor.setDoctorId(doctorId);
        return ApiResponse.success(doctorService.updateProfile(doctor));
    }

    /**
     * 获取医生排班信息
     * @param doctorId
     * @param date
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/{doctorId}/schedules")
    public ApiResponse<PageResponse<DoctorSchedule>> schedules(@PathVariable("doctorId") Long doctorId,
                                                               @RequestParam(name = "date", required = false) String date,
                                                               @RequestParam(name = "page", defaultValue = "1") int page,
                                                               @RequestParam(name = "size", defaultValue = "10") int size) {
        LocalDate d = (date == null || date.isEmpty()) ? null : LocalDate.parse(date);
        return ApiResponse.success(doctorService.listSchedules(doctorId, d, page, size));
    }

    /**
     * 获取医生已分配订单
     * @param doctorId
     * @param status
     * @param orderType
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/{doctorId}/orders")
    public ApiResponse<PageResponse<ExaminationOrder>> assignedOrders(@PathVariable("doctorId") Long doctorId,
                                                                      @RequestParam(name = "status", required = false) Integer status,
                                                                      @RequestParam(name = "orderType", required = false) String orderType,
                                                                      @RequestParam(name = "page", defaultValue = "1") int page,
                                                                      @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(doctorService.listAssignedOrders(doctorId, status, orderType, page, size));
    }

    /**
     * 创建样本
     * @param orderId
     * @param sampleType
     * @param remark
     * @return
     */
    @PostMapping("/samples")
    public ApiResponse<Sample> createSample(@RequestParam(name = "orderId") Long orderId,
                                            @RequestParam(name = "sampleType") String sampleType,
                                            @RequestParam(name = "remark", required = false) String remark) {
        return ApiResponse.success(doctorService.createSample(orderId, sampleType, remark));
    }

    /**
     * 更新样本状态
     * @param sampleId
     * @param status
     * @param remark
     * @return
     */
    @PutMapping("/samples/{sampleId}/status")
    public ApiResponse<Boolean> updateSampleStatus(@PathVariable("sampleId") Long sampleId,
                                                   @RequestParam(name = "status") Integer status,
                                                   @RequestParam(name = "remark", required = false) String remark) {
        return ApiResponse.success(doctorService.updateSampleStatus(sampleId, status, remark));
    }

    /**
     * 记录指标结果
     * @param result
     * @return
     */
    @PostMapping("/indicator-results")
    public ApiResponse<IndicatorResult> recordIndicatorResult(@RequestBody IndicatorResult result) {
        return ApiResponse.success(doctorService.recordIndicatorResult(result));
    }

    /**
     * 更新指标异常状态
     * @param resultId
     * @param abnormal
     * @param comment
     * @return
     */
    @PutMapping("/indicator-results/{resultId}/abnormal")
    public ApiResponse<Boolean> updateIndicatorAbnormal(@PathVariable("resultId") Long resultId,
                                                        @RequestParam(name = "abnormal") Boolean abnormal,
                                                        @RequestParam(name = "comment", required = false) String comment) {
        return ApiResponse.success(doctorService.updateIndicatorResultFlag(resultId, abnormal, comment));
    }
}